/*
 * 
 */
var isOpenDishes = false;
// extend the plugin
( function($) {

	//define the new for the plugin ans how to call it	
	$.fn.contactable = function(options) {
		//set default options 
		var defaults = {
			name : 'Name',
			email : 'Email',
			message : 'Message',
			subject : 'A contactable message',
			recievedMsg : 'Thankyou for your message',
			notRecievedMsg : 'Sorry but your message could not be sent, try again later',
			disclaimer : '',
			hideOnSubmit : true
		};

		// call in the default otions
		var options = $.extend(defaults, options);
		
		// act upon the element that is passed into the design
		return this.each( function(options) {
			//construct the form
				$(this).html('<div id="dishesBox"><div id="dishesTitle"></div><div id="btnMydishes"></div><div id="myDishesList"></div><div  id="buttonCon"></div></div>');
				var title = "<ul style='float:clear;margin-top:0px;'>";
				title += "<li style='background:url(images/dock-bg.gif) repeat-x;' class='th'> ";
				title += "<div class='th1' style='line-height:30px;'>序号</div>";
				title += "<div class='th2' style='line-height:30px;'>菜名</div>";
				title += "<div class='th3' style='line-height:30px;'>价格</div>";
				title += "<div class='th4' style='line-height:30px;'>份数</div>";
				title += "<div class='th5' style='line-height:30px;'>状态</div>";
				title += "<div class='th6' style='line-height:30px;'>+/-</div>";
				title += "</li>";
				title += "</ul>";
				$("#dishesTitle").html(title);
				$('div#btnMydishes').toggle( function() {					
					   isOpenDishes = true;
						$('#dishesBox').animate( {"marginLeft" : "+=600px"}, "slow");
						$(this).css("background",
								"url(./images/scrl_left.png) no-repeat");
						getDishesData();
					} ,function(){
						hideBox();
					} );
				   
			});
	};
})(jQuery);

 function  hideBox() {
	isOpenDishes = false;
	$('#dishesBox').animate( {
		"marginLeft" : "-=600px"
	}, "slow");
	$('#btnMydishes').css("background","url(./images/scrl_right.png) no-repeat");
}

function getDishesData() {
	itemBgFlag = true;
	tblContent = "";
	orderMoney = 0;
	dishesCount = 0;
	getData(parseOrderDishesItem, myOrderListUrl, 'tableId=' + tableId
			+ '&currentPage=' + page + '&size=' + limit);
}
var tblContent = "";
var DataLoadedInterval;
var limit = 100;
var page = 1;
var orderMoney = 0;
var dishesCount = 0;
// 奇偶背景标志
var itemBgFlag = true;
function parsePreOrderDishesItem(json) {
	var total = json.totalPage;
	var list = json.data;
	var listData = "";
	var ids = "";
	var countsList = "";
	var money = 0;
	var y_count = 0; // 预选份数
	if (list != null) {
		var len = 0;
		if (list.length > 0) {
			len = list.length - 1;
		}
		for ( var i = 0; i < list.length; i++) {
			var item = list[i];
			var s = item.dishesId + ",";
			s += item.name + ",";
			s += ",";
			s += item.price + ",";
			if (i != len)
				ids += item.dishesId + ",";
			else
				ids += item.dishesId;

			if (i != len)
				countsList += item.count + ",";
			else
				countsList += item.count;
			money += (item.price * item.count);
			y_count += item.count;
			if (itemBgFlag) {
				tblContent += "<li class='td' style='background:#BBB'>";
				itemBgFlag = false;
			} else {
				tblContent += "<li class='td' style='background:#AAA'>";
				itemBgFlag = true;
			}
			tblContent += "<div class='th1' >预 " + (parseInt(i + 1)) + "</div>";
			tblContent += "<div class='th2' >" + item.name + "</div>";
			tblContent += "<div class='th3' >" + item.price + "</div>";
			tblContent += "<div class='th4' >" + item.count + "</div>";
			tblContent += "<div class='th5' >预选</div>";
			tblContent += "<div class='th6' ><input type='image' src='images/add.png' onclick='preOrder(\""
					+ s + "\")' value='+1'/>";
			tblContent += "<input type='image' src='images/sub.png' onclick='cancelPreOrder(\""
					+ item.dishesId + "\",1)' value='-1'/></div>";
			tblContent += "</li>";
		}
	}
	tblContent += "</ul>";
	$("#myDishesList").html(tblContent);
	var orderStr = "";
	/* 下单栏 */
	orderStr += "<div style='float:left;margin-left:5px;'>预选金额：<span style='color:red'>"
			+ money
			+ "</span>元  &nbsp;&nbsp; 下单金额：<span style='color:red'>"
			+ orderMoney + "</span>元 </div>";
	orderStr += "<div style='float:left'>&nbsp;&nbsp<input  type=\"image\" src=\"images/buy.png\"  onclick='order(\""
			+ ids + "\",\"" + countsList + "\")' /></div>";
	orderStr += "<div style='color:black;' >";
	var dishesTotal = parseInt(dishesCount) + parseInt(y_count);
	var _totalMoney = parseInt(orderMoney) + parseInt(money);
	orderStr += "<br>菜份数：" + dishesTotal + "&nbsp;&nbsp;";
	orderStr += " 总计：<span style='color:red'>" + _totalMoney
			+ "</span>元 </div>";
	$("#buttonCon").html(orderStr);

}

function parseOrderDishesItem(json) {
	var total = json.totalPage;
	var list = json.data;
	var listData = "";
	tblContent += "<ul  id='ulist'>";
	for ( var i = 0; i < list.length; i++) {
		var item = list[i];
		dishesCount += item.count;
		var s = item.dishes.dishesId + ",";
		s += item.dishes.dishesName + ",";
		s += ",";
		s += item.dishes.dishesPrice + ",";
		if (itemBgFlag) {
			tblContent += "<li class='td' style='background:#BBB'>";
			itemBgFlag = false;
		} else {
			tblContent += "<li class='td' style='background:#AAA'>";
			itemBgFlag = true;
		}
		tblContent += "<div class='th1' >" + (parseInt(i + 1)) + "</div>";
		tblContent += "<div class='th2' >" + item.dishes.dishesName + "</div>";
		tblContent += "<div class='th3' >" + item.dishes.dishesPrice + "</div>";
		tblContent += "<div class='th4' >" + item.count + "</div>";
		tblContent += "<div class='th5' >已下单</div>";
		tblContent += "<div class='th6' ><input type='image' src='images/add.png' onclick='preOrder(\""
				+ s + "\")' value='+1'></div>";
		tblContent += "</li>";
		orderMoney += (item.dishes.dishesPrice * item.count);
	}
	getData(parsePreOrderDishesItem, myTmpListUrl, 'tableId=' + tableId
			+ '&currentPage=' + page + '&size=' + limit);
}

function order(ids, counts) {
	//份数也用逗号分开
	if (counts.length == 0) {
		dialog("请选菜，谢谢", 1000);
		return;
	}
	getData(orderRreshMyList, orderUrl, 'dids=' + ids + '&tableId=' + tableId
			+ "&uid=" + uid + "&count=" + counts);

}

function orderRreshMyList() {
	dialog("下单成功", 1000);
	getDishesData();
}

function refreshMyList() {
	getDishesData();
}

function cancelPreOrder(id, count) {
	getData(OrderRefreshList, cancelPreOrderUrl, 'tableId=' + tableId + '&did='
			+ id + '&count=' + count);
}

function OrderRefreshList() {
	dialog("操作成功", 1000);
	getDishesData();
}

var _slideTotalPix = 0;
var _slidePix = 200;
var id = "#ulist";
var isOver = true;
function _upp() {
	var _contentHeight = $(id).height();
	if (isOver) {
		isOver = false;
		if (_contentHeight - _slideTotalPix <= _slidePix) {
			$(id).css( {
				marginTop : "0px"
			});
			_slideTotalPix = 0;
			isOver = true;
			return;
		} else {
			$(id).animate( {
				marginTop : "-=" + _slidePix + "px"
			}, 300, function() {
				_slideTotalPix += _slidePix;
				isOver = true;
			});
		}
	}
}
function _downn() {
	var _contentHeight = $(id).height();
	var top = $(id).css("marginTop");
	if (top == "0px" || top == "auto" || top == "0") {
		return;
	} else {
		$(id).animate( {
			marginTop : "+=" + _slidePix + "px"
		}, 300, function() {
			//_slideTotalPix=0;
			});

	}
}